﻿@inject IJSRuntime JSRuntime

<div @ref="_ref"
     class="masa-table-viewer__header-column-content d-flex justify-space-between @Class"
     @onclick="@HandleCloumOnClick">
    <div class="flex-grow-1 @AlignClass">
        @if (IsRowSelect)
        {
            <MSimpleCheckbox Class="m-data-table__checkbox"
                             Indeterminate="@IsIndeterminate"
                             Value="@IsSelectedAll"
                             ValueChanged="@OnSelectAll"
                             Color="@CheckboxColor" />
        }
        else
        {
            @* @Templates.GenTypeIcon(Data.Type, true) *@
            <span class="text-truncate px-2">@Data.Name</span>
        }

        @if (!Internal && Index > 0)
        {
            <MIcon Small Class="sort-icon">@SortClass</MIcon>
            <span class="m-data-table-header__sort-badge">@Index</span>
        }

        @if (!IsRowSelect && Editable)
        {
            <Dropdown>
                @if (!Internal)
                {
                    <MListItem Title="Edit" PrependIcon="mdi-pencil-outline"
                               OnClick="@(() => OnColumnEditClick.InvokeAsync(Data))"></MListItem>
                }
                <MListItem Title="Hide" PrependIcon="mdi-eye-off-outline"
                           OnClick="@(() => OnColumnToggle.InvokeAsync(Data.Id))"></MListItem>
            </Dropdown>
        }
    </div>
    <div class="justify-end">
        @if (HasFilter)
        {
            <MIcon Small Class="sort-icon">mdi-filter</MIcon>
        }
    </div>
</div>

@if (!Internal)
{
    <Resizor />
}

@code {

    [Parameter] public Column Data { get; set; } = null!;

    [Parameter] public bool Editable { get; set; }

    [Parameter] public Sort? Sort { get; set; }

    [Parameter] public EventCallback<Sort> OnClick { get; set; }

    [Parameter] public EventCallback<Column> OnColumnEditClick { get; set; }

    [Parameter] public EventCallback<string> OnColumnToggle { get; set; }

    [Parameter] public bool IsSelectedAll { get; set; }

    [Parameter] public bool IsIndeterminate { get; set; }

    [Parameter] public EventCallback<bool> OnSelectAll { get; set; }

    [Parameter] public string? CheckboxColor { get; set; }

    [Parameter] public Filter? Filter { get; set; }

    [Parameter] public AlignTypes Align { get; set; } = AlignTypes.Start;

    private ElementReference _ref;

    private SortOption? SortOption => Sort?.Options.FirstOrDefault(u => u.ColumnId == Data.Id);

    private bool IsRowSelect => Data.Type == ColumnType.RowSelect;

    private bool Internal => IsRowSelect || Data.Type is ColumnType.Actions;

    private int Index
    {
        get
        {
            if (SortOption is null || Sort is null || Internal)
            {
                return 0;
            }

            return Sort.Options.OrderBy(u => u.Index).ToList().IndexOf(SortOption) + 1;
        }
    }

    protected override void OnAfterRender(bool firstRender)
    {
        if (firstRender)
        {
            _ = JSRuntime.InvokeVoidAsync(JsInteropConstants.AddStopPropagationEvent, _ref, "pointerdown");
        }
    }

    private string Class
    {
        get
        {
            if (SortOption is null)
            {
                return "";
            }

            return SortOption.OrderBy switch
            {
                SortOrder.Asc => "active asc",
                SortOrder.Desc => "active desc",
                _ => ""
            };
        }
    }

    private string AlignClass
    {
        get
        {
            return Align switch
            {
                AlignTypes.Start => "text-left",
                AlignTypes.Center => "text-center",
                AlignTypes.End => "text-right",
                _ => ""
            };
        }
    }

    private string SortClass
    {
        get
        {
            if (SortOption is null)
            {
                return "";
            }
            return SortOption.OrderBy switch
            {
                SortOrder.Asc => "mdi-arrow-up",
                SortOrder.Desc => "mdi-arrow-down",
                _ => ""
            };
        }
    }

    private bool HasFilter
    {
        get
        {
            return Filter != null && Filter.Options != null && Filter.Options.Any(f => !f.Persistent && (f.ColumnId == Data.Id || Data.Type == ColumnType.Date && Data.Id.StartsWith(f.ColumnId)));
        }
    }

    private async Task HandleCloumOnClick()
    {
        if (Internal)
        {
            return;
        }

        var sort = Sort ?? new();

        if (SortOption is null)
        {
            var sortOption = new SortOption
            {
                ColumnId = Data.Id,
                OrderBy = SortOrder.Asc,
                Type = Data.Type.ConvertToExpectedType(),
                Index = sort.Options.MaxBy(u => u.Index)?.Index + 1 ?? 0
            };

            sort.Options.Add(sortOption);
        }
        else if (SortOption.OrderBy == SortOrder.Asc)
        {
            SortOption.OrderBy = SortOrder.Desc;
        }
        else
        {
            sort.Options.Remove(SortOption);
        }

        await OnClick.InvokeAsync(sort);
    }
}